/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on Nov 11, 2011, 9:11:57 AM
 */
package wte.rnd.buildingontology.gui;

import java.io.IOException;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import wte.rnd.buildingontology.core.FileImportProcess;
import wte.rnd.buildingontology.core.OntologyModel;

import edu.stanford.smi.protegex.owl.jena.JenaOWLModel;
import edu.stanford.smi.protegex.owl.model.OWLNamedClass;
import wte.rnd.buildingontology.gui.ClassTreePanel;
import wte.rnd.buildingontology.core.EntityInfo;
/**
 *
 * @author linhvtt
 */
public class MainFrame extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form MainFrame 
     * @throws UnsupportedLookAndFeelException 
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     * @throws ClassNotFoundException */

    public MainFrame() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
    	
    	listExistingEntities = new ArrayList<EntityInfo>();

    	listChosenClassEntities = new ArrayList<EntityInfo>();

    	
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")    
    private void initComponents() {
    	

    	this.owlModel = null;
    	enInfo=new ShowEntityInfo();

        buttonGroup1 = new javax.swing.ButtonGroup();
        
        jInternalFrame1 = new javax.swing.JInternalFrame();
        
        jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        
        existingEntityList = new javax.swing.JList();   
        catchedEntityList = new javax.swing.JList();
        
        jSplitPane1 = new javax.swing.JSplitPane();
        jSplitPane2 = new javax.swing.JSplitPane();
        jSplitPane3 = new javax.swing.JSplitPane();
        jSplitPane4 = new javax.swing.JSplitPane();        
        jSplitPane5 = new javax.swing.JSplitPane();
        
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();

        jPanel8 = new javax.swing.JPanel();
        jPanel9 = new javax.swing.JPanel();
        
        jScrollPane4 = new javax.swing.JScrollPane();
        jScrollPane5 = new javax.swing.JScrollPane();
        
        chosenClassEntity = new javax.swing.JList();
        
        entityNameLabel = new javax.swing.JLabel();
        entityClassLabel = new javax.swing.JLabel();
        entityIDLabel = new javax.swing.JLabel();

        entityNameText = new javax.swing.JTextField();
        entityClassText = new javax.swing.JTextField();
        entityIDText = new javax.swing.JTextField();
        
        propertiesTable = new javax.swing.JTable();
       
        applyButton = new javax.swing.JButton();
        SettingButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        entityRadio = new javax.swing.JRadioButton();
        classRadio = new javax.swing.JRadioButton();

        ImportButton = new javax.swing.JButton();
        
        browserPanel = new SimpleBrowserPanel();

        jInternalFrame1.setVisible(true);

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jSplitPane1.setDividerLocation(500);

        jSplitPane2.setDividerLocation(400);
        jSplitPane2.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane3.setDividerLocation(250);

        catchedEntityList.setModel(catchedEntityModel);            
        catchedEntityList.setValueIsAdjusting(true);
        catchedEntityList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                catchedEntityListMouseClicked(evt);
            }            
        });
        catchedEntityList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                catchedEntityListKeyPressed(evt);
            }
        });
       
        jScrollPane1.setViewportView(catchedEntityList);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
        );

        jSplitPane3.setLeftComponent(jPanel2);
        
        existingEntityList.setModel(existingEntityModel);
        
        existingEntityList.setValueIsAdjusting(true);
        existingEntityList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                existingEntityListMouseClicked(evt);
            }
        });
        existingEntityList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                existingEntityListKeyPressed(evt);
            }
        });
        jScrollPane2.setViewportView(existingEntityList);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 47, Short.MAX_VALUE)
        );

        jSplitPane3.setRightComponent(jPanel3);

        jSplitPane2.setRightComponent(jSplitPane3);
        
        jSplitPane2.setLeftComponent(browserPanel);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 274, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(4, 4, 4)
                    .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 270, Short.MAX_VALUE)))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 393, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jSplitPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE))
        );

        jSplitPane1.setLeftComponent(jPanel1);

        jSplitPane4.setDividerLocation(400);
        jSplitPane4.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);

        jSplitPane5.setDividerLocation(250);

        chosenClassEntity.setValueIsAdjusting(true);
        chosenClassEntity.setModel(chosenEntityModel);
        chosenClassEntity.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                chosenClassEntityMouseClicked(evt);
            }
        });
        jScrollPane4.setViewportView(chosenClassEntity);

        javax.swing.GroupLayout jPanel8Layout = new javax.swing.GroupLayout(jPanel8);
        jPanel8.setLayout(jPanel8Layout);
        jPanel8Layout.setHorizontalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE)
        );
        jPanel8Layout.setVerticalGroup(
            jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
        );

        jSplitPane5.setRightComponent(jPanel8);
        
        JTextArea tx = new JTextArea();
        JScrollPane tree = new JScrollPane(tx);
		ontologyTreePanel = new JPanel(); 
		
		javax.swing.GroupLayout ontologyTreePanelLayout = new javax.swing.GroupLayout(ontologyTreePanel);
		ontologyTreePanel.setLayout(ontologyTreePanelLayout);
		ontologyTreePanelLayout.setHorizontalGroup(
				ontologyTreePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(tree,javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
		);
		ontologyTreePanelLayout.setVerticalGroup(
				ontologyTreePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(tree,javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
		);

		jSplitPane5.setLeftComponent(ontologyTreePanel);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
        );

        jSplitPane4.setTopComponent(jPanel6);

        entityNameLabel.setText("Tên thực thể");

        entityClassLabel.setText("Lớp thực thể");

        entityIDLabel.setText("ID");
               
        entityNameText.getDocument().addDocumentListener(new DocumentListener() {
        	  public void changedUpdate(DocumentEvent e) {
        	    warn();
        	  }
        	  public void removeUpdate(DocumentEvent e) {
        	    warn();
        	  }
        	  public void insertUpdate(DocumentEvent e) {
        	    warn();
        	  }

        	  public void warn() {
        		  entityNameTextKeyPressed();
        	  }
        	});


        propertiesTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Khóa", "Giá trị"
            }
        ));
        jScrollPane5.setViewportView(propertiesTable);

        applyButton.setText("Apply");
        applyButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                applyButtonActionPerformed(evt);
            }
        });

        SettingButton.setText("Save");
        SettingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SettingButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Load Ontology");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        buttonGroup1.add(entityRadio);
        entityRadio.setText("Thực thể");
        entityRadio.setSelected(true);
        entityRadio.setSelected(true);
        entityRadio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                entityRadioActionPerformed(evt);
            }
        });

        buttonGroup1.add(classRadio);
        classRadio.setText("Lớp");
        classRadio.setVisible(false);

        ImportButton.setText("Import...");
        ImportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					ImportButtonActionPerformed(evt);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
        entityClassText.setEditable(false);
        entityIDText.setEditable(true);
        javax.swing.GroupLayout jPanel9Layout = new javax.swing.GroupLayout(jPanel9);
        jPanel9.setLayout(jPanel9Layout);
        jPanel9Layout.setHorizontalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel9Layout.createSequentialGroup()
                                .addComponent(entityRadio)
                                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel9Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(entityNameText, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                                            .addComponent(entityClassText, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
                                            .addComponent(entityIDText, javax.swing.GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)))
                                    .addGroup(jPanel9Layout.createSequentialGroup()
                                        .addGap(52, 52, 52)
                                        .addComponent(classRadio))))
                            .addGroup(jPanel9Layout.createSequentialGroup()
                                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(entityIDLabel)
                                    .addComponent(entityClassLabel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 134, Short.MAX_VALUE)
                                    .addComponent(entityNameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(240, 240, 240)))
                        .addContainerGap())
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(ImportButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(applyButton, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(SettingButton, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        jPanel9Layout.setVerticalGroup(
            jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel9Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(entityIDLabel)
                    .addGroup(jPanel9Layout.createSequentialGroup()
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(entityNameLabel)
                            .addComponent(entityNameText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(entityClassLabel)
                            .addComponent(entityClassText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(entityIDText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(7, 7, 7)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(entityRadio)
                    .addComponent(classRadio))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel9Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ImportButton)
                    .addComponent(applyButton)
                    .addComponent(SettingButton)
                    .addComponent(cancelButton)))
        );

        jSplitPane4.setRightComponent(jPanel9);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 396, Short.MAX_VALUE)
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(jPanel5);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>

private void catchedEntityListMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
    if (evt.getClickCount()>=2)
    {
        System.out.println("catched entity double click on " + catchedEntityList.getSelectedValue());
        int selectedIndex=catchedEntityList.getSelectedIndex();
        EntityInfo entity=new EntityInfo(); 
        entity.setText((String) catchedEntityList.getSelectedValue());
        listExistingEntities.add(entity);
        repaintExistingListEntitites();        
        //listEnableEntities.remove(selectedIndex);
        //catchedEntityModel.remove(selectedIndex);
    }
    
}

private void existingEntityListMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
    if (evt.getClickCount()==1)
    {
        //System.out.println("existingEntityList entity double click on " + existingEntityList.getSelectedValue());
    	currList=existingEntityList.getSelectedIndex();  
        EntityInfo entity=listExistingEntities.get(currList);        
        entity.setClass(entityClassText.getText());        
        currEntity=entity;    
        this.entityNameText.setText((String) existingEntityList.getSelectedValue());    
        this.entityIDText.setText(MainFrame.entityNameText.getText().toLowerCase().replace(' ', '_'));
        //this.entityIDText.setText("");   
    }
    else if (evt.getClickCount()>=2)
    {   
    	String name=existingEntityList.getSelectedValue().toString();
    	//System.out.println("Name "+ name);
    	name.trim();    
    	//System.out.println(OntologyModel.searchIndividual(name,owlModel));
    	enInfo.setList(OntologyModel.searchIndividual(name,owlModel));    	
    	enInfo.initTableModel();
    	enInfo.setVisible(true);
    	enInfo.repaint();
    	
    }
}

private void chosenClassEntityMouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
    if (evt.getClickCount()>=2)
    {
        System.out.println("chosenClassEntity entity double click on " + chosenClassEntity.getSelectedValue());
        currList=chosenClassEntity.getSelectedIndex();  
        EntityInfo entity=listChosenClassEntities.get(currList);        
        //entity.setClass(entityClassText.getText());        
        currEntity=entity;
        this.entityNameText.setText(entity.getText());
        this.entityClassText.setText(entity.getEntityClass());
        this.entityIDText.setText(entity.getID());
    }
}

private void SettingButtonActionPerformed(java.awt.event.ActionEvent evt) {
	try {
		JFileChooser fc = new JFileChooser();
		int result = fc.showSaveDialog(this);
		if(result == JFileChooser.APPROVE_OPTION){
			String url = fc.getSelectedFile().getAbsolutePath();
			System.out.println(url);
			boolean resultr = OntologyModel.saveModelToFileXML(url, owlModel);
			if(resultr){
				JOptionPane.showMessageDialog(this, "Save sucees");
			}else{
				JOptionPane.showMessageDialog(this, "Save false");
			}
		}		
	} catch (Exception e) {
		JOptionPane.showMessageDialog(null, e.getMessage());
	}
}
private void ImportButtonActionPerformed(java.awt.event.ActionEvent evt) throws IOException {
	JFileChooser fc = new JFileChooser();
	int result = fc.showOpenDialog(this);
	if(result == JFileChooser.APPROVE_OPTION)
	{
		String path = fc.getSelectedFile().getAbsolutePath();
		ArrayList<String> list = FileImportProcess.getListIndividualFormFile(path);
		for (int i=0;i<list.size();++i)
		{
			EntityInfo entity=new EntityInfo();
			entity.setText(list.get(i));
			listExistingEntities.add(entity);
		}
		this.repaintExistingListEntitites();
	}	
}


private void applyButtonActionPerformed(java.awt.event.ActionEvent evt) {
	TreeNode node = tree.getSelectTreeNode();
	OntologyModel.insertidInstanceToOWLModel(this.entityNameText.getText(), this.entityIDText.getText(), node.getIdClss(), this.owlModel);
	EntityInfo a = new EntityInfo();
	a.setID(this.entityIDText.getText());
	a.setText(this.entityNameText.getText());
	a.setClass(node.getNameClss());
	OWLNamedClass owlNamedClass = OntologyModel.queryOWLClassByNamed(this.tree.getSelectTreeNode().getIdClss(), owlModel);
	int count = owlNamedClass.getInstanceCount(false);
	this.entityIDText.setText(entityNameText.getText().toLowerCase());
	this.listChosenClassEntities.add(a);
	this.entityNameText.setText("");
	repaintFilesListEntitites();
}

private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
	try {
		JFileChooser fc = new JFileChooser();
		int result = fc.showOpenDialog(this);
		if(result == JFileChooser.APPROVE_OPTION){
			String url = fc.getSelectedFile().getAbsolutePath();
			this.owlModel = OntologyModel.loadOntologyModel(url);
			
			if(this.owlModel != null){
				tree = new ClassTreePanel(this.owlModel);
				ontologyTreePanel = new JPanel(); 
				javax.swing.GroupLayout ontologyTreePanelLayout = new javax.swing.GroupLayout(ontologyTreePanel);
				ontologyTreePanel.setLayout(ontologyTreePanelLayout);
				ontologyTreePanelLayout.setHorizontalGroup(
						ontologyTreePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addComponent(tree.getTreePanel(),javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
				);
				ontologyTreePanelLayout.setVerticalGroup(
						ontologyTreePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
						.addComponent(tree.getTreePanel(),javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
				);

				jSplitPane5.setLeftComponent(ontologyTreePanel);
			}else {
				JOptionPane.showMessageDialog(this, "Load ontology false");
			}
		}		
	} catch (Exception e) {
		JOptionPane.showMessageDialog(null, e.getMessage());
	}
}

private void existingEntityListKeyPressed(java.awt.event.KeyEvent evt) {
	// TODO add your handling code here:
	//System.out.println("Delete " + evt.getID());
	if (evt.getID()==401)
	{
		//System.out.println("Delete " + existingEntityList.getSelectedValue());		
		int index=existingEntityList.getSelectedIndex();
		listExistingEntities.remove(index);
		repaintExistingListEntitites();
	}
}

private void catchedEntityListKeyPressed(java.awt.event.KeyEvent evt) {
	// TODO add your handling code here:
	if (evt.getID()==401)
	{
		//System.out.println("Delete " + existingEntityList.getSelectedValue());
		int index=listEnableEntities.indexOf(catchedEntityList.getSelectedValue());
		listEnableEntities.remove(index);
		repaintCatchListEntitites();
	}
}

private void entityRadioActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void entityNameTextKeyPressed() {
	// TODO add your handling code here:
	//System.out.println("char "+ evt.getKeyChar());
	//System.out.println("code "+ evt.getKeyCode());
	//System.out.println("id "+ evt.getID());
	    		int index=currList;	    	
	    		System.out.println("index "+ index);	    		
	    	    String newValue=entityNameText.getText();	    	    
	    	    System.out.println("newValue "+ newValue);
	    		EntityInfo entity= currEntity;
	    		entity.setText(newValue);
	    		this.repaintExistingListEntitites();
	    		this.repaintFilesListEntitites();
	    		existingEntityList.setSelectedIndex(index);
	    	
	    
	}
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
					new MainFrame().setVisible(true);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedLookAndFeelException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
    } 
    public static JenaOWLModel owlModel;
    public static ArrayList<String> listEnableEntities;
    public static ArrayList<EntityInfo> listChosenClassEntities;
    public static ArrayList<EntityInfo> listExistingEntities;
    
    private static int currList;
    private static EntityInfo currEntity;
    // Variables declaration - do not modify
    private javax.swing.JButton ImportButton;
    private javax.swing.JButton SettingButton;
    private javax.swing.JButton applyButton;
    private javax.swing.JPanel browserPanel;
    private javax.swing.ButtonGroup buttonGroup1;    
    private javax.swing.JButton cancelButton;
    public static javax.swing.JList catchedEntityList;
    private javax.swing.JList chosenClassEntity;
    private javax.swing.JRadioButton classRadio;
    private javax.swing.JLabel entityClassLabel;
    public static javax.swing.JTextField entityClassText;
    private javax.swing.JLabel entityIDLabel;
    public static javax.swing.JTextField entityIDText;
    private javax.swing.JLabel entityNameLabel;
    public static javax.swing.JTextField entityNameText;
    private javax.swing.JRadioButton entityRadio;
    private javax.swing.JList existingEntityList;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel ontologyTreePanel;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JSplitPane jSplitPane3;
    private javax.swing.JSplitPane jSplitPane4;
    private javax.swing.JSplitPane jSplitPane5;
    private javax.swing.JTable propertiesTable;
    private ClassTreePanel tree;
    private ShowEntityInfo enInfo;

    static DefaultListModel catchedEntityModel = new DefaultListModel();
    static DefaultListModel chosenEntityModel = new DefaultListModel();
    static DefaultListModel existingEntityModel = new DefaultListModel();
    public static void repaintCatchListEntitites()

    {
    	catchedEntityModel.clear();
    	for (int i=0;i<listEnableEntities.size();++i)
    	{
    		catchedEntityModel.add(i, listEnableEntities.get(i));
    	}
    	//this.catchedEntityList.setModel(catchedEntityModel);
    }

    static void repaintFilesListEntitites()

    {
    	chosenEntityModel.clear();
    	for (int i=0;i<listChosenClassEntities.size();++i)
    	{
    		chosenEntityModel.add(i, "(" + listChosenClassEntities.get(i).getID() + ") \t" + listChosenClassEntities.get(i).getText());
    	}
    }
    public static void repaintExistingListEntitites()
    {
    	existingEntityModel.clear();
    	for (int i=0;i<listExistingEntities.size();++i)
    	{
    		existingEntityModel.add(i, listExistingEntities.get(i).getText());
    	}
    }
    
    public class ShowEntityInfo extends javax.swing.JFrame {

        /** Creates new form ShowEntityInfo */
        public ShowEntityInfo() {
        	
            initComponents(); 
            entityList=new ArrayList<EntityInfo>();
        }
        
        public void setList(ArrayList<EntityInfo> list)
        {
        	entityList=list;
        }

        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initTableModel()
        { 	      	
        	tableModel=new javax.swing.table.DefaultTableModel(
                    new String [][] {

                    },
                    new String [] {
                        "Tên", "Lớp"
                    }
                ) {
                    Class[] types = new Class [] {
                        java.lang.String.class, java.lang.String.class
                    };
                    boolean[] canEdit = new boolean [] {
                        false, false
                    };

                    public Class getColumnClass(int columnIndex) {
                        return types [columnIndex];
                    }

                    public boolean isCellEditable(int rowIndex, int columnIndex) {
                        return canEdit [columnIndex];
                    }
                };
              if (entityList!=null)       
              //System.out.println(entityList.get(0).getText()+" "+ entityList.get(0).getEntityClass());
              for (int i=0;i<entityList.size();++i)
              {
            	  tableModel.addRow(new Object[] {entityList.get(i).getText(),entityList.get(i).getEntityClass()});
            	  //tableModel.addRow(new Object[] {"demo","demo"});
              }
              EntitiesTable.setModel(tableModel);
        	
        }
        private void initComponents() {

            jPanel1 = new javax.swing.JPanel();
            jScrollPane1 = new javax.swing.JScrollPane();
            EntitiesTable = new javax.swing.JTable();
            jLabel1 = new javax.swing.JLabel();
            CloseButton = new javax.swing.JButton();

            setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
            this.setLocation(300, 250);
            //initTableModel();    
            tableModel=new javax.swing.table.DefaultTableModel();    
            EntitiesTable.setModel(tableModel);
            
            EntitiesTable.getTableHeader().setReorderingAllowed(false);
            EntitiesTable.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    EntitiesTableMouseClicked(evt);
                }
            });
            jScrollPane1.setViewportView(EntitiesTable);

            jLabel1.setText("Thực thể cùng tên");

            CloseButton.setText("Close");
            CloseButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    CloseButtonActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
            jPanel1.setLayout(jPanel1Layout);
            jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addGap(10, 10, 10)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap())
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                    .addContainerGap(331, Short.MAX_VALUE)
                    .addComponent(CloseButton)
                    .addContainerGap())
            );
            jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(CloseButton)
                    .addGap(33, 33, 33))
            );

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
            );

            pack();
        }// </editor-fold>

    private void CloseButtonActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    	this.hide();
    }

    private void EntitiesTableMouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
        System.out.println("Mouse click entities table");
    }

        /**
         * @param args the command line arguments
         */        
    	private ArrayList<EntityInfo> entityList;
        // Variables declaration - do not modify
        private javax.swing.JButton CloseButton;
        private javax.swing.JTable EntitiesTable;
        private javax.swing.JLabel jLabel1;
        private javax.swing.JPanel jPanel1;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.table.DefaultTableModel tableModel;
        // End of variables declaration
    }
    // End of variables declaration
}
